#include "node.hpp"
#include <memory>
using namespace std;

namespace footman {
    uint32_t Node::__EntityIdCounter = 0;

    Node::Node()
        :entityId( __EntityIdCounter++ )
    {
        
        _components.fill( nullptr );
    }

    Node::~Node()
    {
        for ( auto com : _components ) {
            if ( Object::IsValid( com ) ) {
                com->destroy();
            }
        }

        for ( auto child : children ) {
            delete child;
        }
    }

    void Node::addChild( Node& child ) {
        assert( !child.parent );
        child.parent = this;
        children.push_back( &child );
    }

    void Node::removeChild( Node const & child ) {
        auto iter = children.begin();
        auto end = children.end();
        while ( iter != end ) {
            if ( ( *iter ) == &child ) {
                children.erase( iter );
                break;
            }
            iter++;
        }
    }

}